package net.ithe.excption;

import lombok.extern.slf4j.Slf4j;
import net.ithe.pojo.Result;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public Result handleException(Exception ex){
        log.error("程序出错了", ex);
        return Result.error("服务器出错了~");
    }
    @ExceptionHandler
    public Result handleDuplicateKeyException(DuplicateKeyException ex){
        log.error("程序出错了", ex);
        String msg = ex.getMessage();
        int i = msg.indexOf("Duplicate entry");
        String errorMsg= msg.substring(i);
        String[] arr=  errorMsg.split(" ");
        return Result.error(arr[2]+"已存在");
    }
}
